﻿using System;
using System.Collections.Generic;
using FireStarterModeller.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI.LookupLists
{
    public class LookupTypeSelectorControl : UserControlWin
    {
        private DMProperty _dmProperty;
        public IFireStarterFormsController FireStarterFormsController { get; private set; }
        public List<ILookupListControl> LookupTypeControls { get; private set; }
        public IPanel LookupTypeControlPanel { get; private set; }
        public IComboBox LookupTypeComboBox { get; private set; }

        public LookupTypeSelectorControl(IFireStarterFormsController fireStarterFormsController)
        {
            FireStarterFormsController = fireStarterFormsController;
            LookupTypeControls = new List<ILookupListControl>();
            LookupTypeControls.Add(fireStarterFormsController.CreateNoneLookupTypeControl());
            LookupTypeControls.Add(fireStarterFormsController.CreateSimpleLookupTypeControl());
//            LookupTypeControls.Add(fireStarterFormsController.CreateDatabaseLookupTypeControl());
            LookupTypeControls.Add(fireStarterFormsController.CreateBusinessObjectLookupTypeControl());
            this.Dock = (System.Windows.Forms.DockStyle) DockStyle.Fill;
            IControlFactory controlFactory = FireStarterFormsController.ControlFactory;
            LookupTypeComboBox = SetupComboBox(controlFactory);
            LookupTypeControlPanel = controlFactory.CreatePanel();
            LookupTypeControlPanel.Dock = DockStyle.Fill;
            IPanel lookupTypeComboBoxPanel = controlFactory.CreatePanel();
            lookupTypeComboBoxPanel.Height = 25;
            GridLayoutManager gridLayoutManager = new GridLayoutManager(lookupTypeComboBoxPanel, controlFactory);
            gridLayoutManager.SetGridSize(1,2);
            gridLayoutManager.AddControl(controlFactory.CreateLabel("Select Lookup Type"));
            gridLayoutManager.AddControl(LookupTypeComboBox);
            //gridLayoutManager.AddControl(LookupTypeControlPanel,1,2);
            //gridLayoutManager.FixColumnBasedOnContents(0);
            //gridLayoutManager.FixAllRowsBasedOnContents();
            BorderLayoutManager manager = controlFactory.CreateBorderLayoutManager(this);
            manager.AddControl(lookupTypeComboBoxPanel, BorderLayoutManager.Position.North);
            manager.AddControl(LookupTypeControlPanel, BorderLayoutManager.Position.Centre);
        }

        private IComboBox SetupComboBox(IControlFactory controlFactory)
        {
            IComboBox comboBox = controlFactory.CreateComboBox();
            comboBox.SelectedIndexChanged += ComboBox_OnSelectedIndexChanged;
            comboBox.Items.Clear();
            foreach (ILookupListControl lookupTypeControl in LookupTypeControls)
            {
                comboBox.Items.Add(lookupTypeControl.LookupTypeName);
            }
            return comboBox;
        }

        private void ComboBox_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedItem = LookupTypeComboBox.SelectedItem as string;
            ILookupListControl foundLookupListControl = LookupTypeControls.Find(control => control.LookupTypeName == selectedItem);
            SetCurrentLookupTypeControl(foundLookupListControl);
            if (DMProperty != null)
            {
                DMLookupList lookupList = null;
                if (foundLookupListControl != null) lookupList = foundLookupListControl.LookupList;
                DMProperty.LookupList = lookupList;
            }
        }

        private void SetCurrentLookupTypeControl(ILookupListControl lookupListControl)
        {
            LookupTypeControlPanel.Controls.Clear();
            if (lookupListControl != null)
            {
                lookupListControl.Dock = DockStyle.Fill;
                LookupTypeControlPanel.Controls.Add(lookupListControl);
            }
        }

        public DMProperty DMProperty
        {
            get { return _dmProperty; }
            set
            {
                _dmProperty = value;
                foreach (ILookupListControl lookupTypeControl in LookupTypeControls)
                {
                    lookupTypeControl.Property = _dmProperty;
                }
                bool hasProperty = _dmProperty != null;
                string lookupTypeNameToSelect = null;
                if (hasProperty)
                {
                    ILookupListControl foundLookupListControl = LookupTypeControls.Find(
                        control => control.LookupList == _dmProperty.LookupList);
                    if (foundLookupListControl != null)
                        lookupTypeNameToSelect = foundLookupListControl.LookupTypeName;
                }
                LookupTypeComboBox.SelectedItem = lookupTypeNameToSelect;
                this.Enabled = hasProperty;
                LookupTypeComboBox.Enabled = hasProperty;
                LookupTypeControlPanel.Enabled = hasProperty;
            }
        }
    }
}
